package app.Disthanitian.thaistyle.EmerMez;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import app.Disthanitian.thaistyle.EmerMez.AddRiskZone.WebPageTask;

public class UpdateTimeLocate {
	
	static UrlEmerMez URLlink = new UrlEmerMez();
	public static final String urlUpdate = URLlink.getUrlupdatetime();
	MemberUser user = new MemberUser();
	String user_id = user.getId();
	//int timeInt = user.getTimeLocateUpdate();
	int timeInt;
	String timeSt ;
	public static String result = "";
	 
	public void callRun(){
		
		 timeInt =user.getTimeLocateUpdate();
		
		WebPageTask task = new WebPageTask();
		task.execute();
	}
	
	
	public String  UpdateTime() {
		timeSt = Integer.toString(timeInt);

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("userid", user_id));
		nameValuePairs.add(new BasicNameValuePair("time", timeSt));
		Log.d(timeSt, "Error" + timeInt);


		return postData(urlUpdate, nameValuePairs);

	}

	public String postData(String url, List<NameValuePair> nameValuePairs) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");
		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));

			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			String page = sb.toString();
			return page;
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}
		return null;
	}

	protected class WebPageTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
		
			result =UpdateTime() ;
			return result;
		}

		protected void onPostExecute(String result) {
					
			if (result.trim().equals("ok")) {
			       
				} 

		}
	
            
}
}
